﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NumberLine : MonoBehaviour
{
    public CallbackInt CallbackBuyNumber;
    public NumberBoxChooseLine m_ChooseLine;
    public List<NumberBox> m_NumberBoxs = new List<NumberBox>();
    public NumberBox m_NumberBoxPerfab;
    public int m_Bits = 4;
    void Start()
    {
        for(int i=0; i< m_Bits; ++i)
        {
            var box = Instantiate(m_NumberBoxPerfab);
            box.transform.SetParent(transform);
            box.transform.localScale = new Vector3(1,1,1);
            box.CallbackNumberBoxClicked = OnNumberBoxClicked;
            box.SetBackColor(new Vector4(1,1,1,1));
            box.SetTextColor(new Vector4(0.2f, 0.6f, 0.2f, 1));
            m_NumberBoxs.Add(box);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void OnNumberBoxClicked(NumberBox box)
    {
        m_ChooseLine.m_ChooseBox = box;
        m_ChooseLine.gameObject.SetActive(true);
    }
    public void OnBuy()
    {
        int number = 0;
        for (int i = 0; i < m_Bits; ++i)
        {
            number += m_NumberBoxs[i].Number * (int)Mathf.Pow(10, m_Bits - i - 1);
            m_NumberBoxs[i].Number = 0;
        }
        if (CallbackBuyNumber != null)
            CallbackBuyNumber(number);
    }
}
